part of '../../repository.dart';

const _kWebPurchasedSubscriptionInfoYookassaPaymentSystemValue = 'yookassa';

class _YookassaPurchasedSubscriptionInfoDto extends Equatable
    implements _WebPurchasedSubscriptionInfoDto {
  const _YookassaPurchasedSubscriptionInfoDto._internal({
    required this.userId,
    required this.purchaseItemSku,
    required this.originalTransactionId,
    required this.autoRenewalEnabled,
    required this.currentRenewalAttemptTimeDtos,
    required this.test,
  });

  @override
  final String userId;

  @override
  final String purchaseItemSku;

  @override
  final String originalTransactionId;

  @override
  final bool autoRenewalEnabled;

  @override
  final BuiltList<_TimeDto> currentRenewalAttemptTimeDtos;

  @override
  final bool test;

  @override
  String get paymentSystem => _kWebPurchasedSubscriptionInfoYookassaPaymentSystemValue;

  @override
  List<Object?> get props {
    return [
      userId,
      purchaseItemSku,
      originalTransactionId,
      autoRenewalEnabled,
      currentRenewalAttemptTimeDtos,
      test,
    ];
  }

  factory _YookassaPurchasedSubscriptionInfoDto.fromJson(Map<String, dynamic> json) {
    final currentRenewalAttemptTimeData =
        json[_kWebPurchasedSubscriptionInfoCurrentRenewalAttemptTimesKey] as List;
    final currentRenewalAttemptTimeStrings =
        List.castFrom<dynamic, String>(currentRenewalAttemptTimeData);
    final currentRenewalAttemptTimeDtos = currentRenewalAttemptTimeStrings.map((timeString) {
      return _TimeDto.fromString(timeString);
    }).toBuiltList();

    return _YookassaPurchasedSubscriptionInfoDto._internal(
      userId: json[_kWebPurchasedSubscriptionInfoUserIdKey],
      purchaseItemSku: json[_kWebPurchasedSubscriptionInfoPurchaseItemSkuKey],
      originalTransactionId: json[_kWebPurchasedSubscriptionInfoOriginalTransactionIdKey],
      autoRenewalEnabled: json[_kWebPurchasedSubscriptionInfoAutoRenewalEnabledKey],
      currentRenewalAttemptTimeDtos: currentRenewalAttemptTimeDtos,
      test: json[_kWebPurchasedSubscriptionInfoTestKey],
    );
  }

  factory _YookassaPurchasedSubscriptionInfoDto.fromEntity(
      YookassaPurchasedSubscriptionInfo purchasedSubscriptionInfo) {
    final currentRenewalAttemptTimeDtos =
        purchasedSubscriptionInfo.currentRenewalAttemptTimes.map((time) {
      return _TimeDto.fromEntity(time);
    }).toBuiltList();

    return _YookassaPurchasedSubscriptionInfoDto._internal(
      userId: purchasedSubscriptionInfo.userId,
      purchaseItemSku: purchasedSubscriptionInfo.purchaseItemSku,
      originalTransactionId: purchasedSubscriptionInfo.originalTransactionId,
      autoRenewalEnabled: purchasedSubscriptionInfo.autoRenewalEnabled,
      currentRenewalAttemptTimeDtos: currentRenewalAttemptTimeDtos,
      test: purchasedSubscriptionInfo.test,
    );
  }

  @override
  YookassaPurchasedSubscriptionInfo toEntity() {
    final currentRenewalAttemptTimes = currentRenewalAttemptTimeDtos.map((timeDto) {
      return timeDto.toEntity();
    }).toBuiltList();

    return YookassaPurchasedSubscriptionInfo(
      userId: userId,
      purchaseItemSku: purchaseItemSku,
      originalTransactionId: originalTransactionId,
      autoRenewalEnabled: autoRenewalEnabled,
      currentRenewalAttemptTimes: currentRenewalAttemptTimes,
      test: test,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    final currentRenewalAttemptTimeStrings = currentRenewalAttemptTimeDtos.map((timeDto) {
      return timeDto.toString();
    }).toList();

    return {
      _kWebPurchasedSubscriptionInfoPaymentSystemKey: paymentSystem,
      _kWebPurchasedSubscriptionInfoUserIdKey: userId,
      _kWebPurchasedSubscriptionInfoPurchaseItemSkuKey: purchaseItemSku,
      _kWebPurchasedSubscriptionInfoOriginalTransactionIdKey: originalTransactionId,
      _kWebPurchasedSubscriptionInfoAutoRenewalEnabledKey: autoRenewalEnabled,
      _kWebPurchasedSubscriptionInfoCurrentRenewalAttemptTimesKey: currentRenewalAttemptTimeStrings,
      _kWebPurchasedSubscriptionInfoTestKey: test,
    };
  }
}
